﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;

namespace ViaConvert.Test.Web.Controllers
{
    using ViaConvert.Test.Web.Models;

    /// <summary>
    /// Api 2 Based
    /// </summary>
    public class Convert2Controller : ApiController
    {
        #region Default
        private HttpResponseException responseException;
        
        private Message message;

        public Convert2Controller()
        {
            message = new Message();
        }
        #endregion

        #region Api Capture
        public async Task<Message> Post()
        {
            IsMimeMultipartContent();
            ReadContent();
            return message;
        }
        #endregion

        #region Method
        /// <summary>
        /// Check Mime Type
        /// </summary>
        /// <returns></returns>
        private Message IsMimeMultipartContent()
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                responseException = new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
                message.responseObject = responseException;
                returnMessage("File Media Type Error", "Fail", responseException);
            }
            else
            {
                responseException = new HttpResponseException(HttpStatusCode.Accepted);
                returnMessage("File Accepted", "Ok", responseException);
            }

            return message;
        }

        private void ReadContent()
        {
            var data = Request.Content.ReadAsStreamAsync().Result;
           
            
            string content = "";


            Stream stream = data;

            StreamReader streamReader = new StreamReader(stream);
            string Head, File, Type, Blank = null;

            Head = streamReader.ReadLine();
            File = streamReader.ReadLine();
            Type = streamReader.ReadLine();
            Blank = streamReader.ReadLine();


            content = streamReader.ReadToEnd();


            #region Proxy 
            var ret = Request.Content.ReadAsStringAsync()
                .ContinueWith(x =>
                {
                    var context = new StringContent(x.Result);
                    return Proxy(context);
                }).Unwrap();
            #endregion
            
            returnMessage("File Read", content , "" );
        }
        private Task<HttpResponseMessage> Proxy(HttpContent context)
        {
            
            return null;
        }



        #region Local Method
        private Message returnMessage(string statusText)
        {
            message.statusText = statusText;

            return message;
        }

        private Message returnMessage(string statusText, string responseText, object responseObject)
        {
            message.responseObject = responseObject;
            message.responseText = responseText;
            message.statusText = statusText;

            return message;
        }

        private Message returnMessage(string statusText, string responseText, object responseObject, int readyState, int Status)
        {
            message.readyState = readyState;
            message.responseObject = responseObject;
            message.responseText = responseText;
            message.Status = Status;
            message.statusText = statusText;

            return message;
        }
        #endregion


        #endregion

    }
}
